using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.PageFlow.Utils;

namespace WCSFContrib.PageFlow.Xml.Storage.EnterpriseLibrary.Util
{
    public static class SerializationHelper
    {
        #region Binary Serializers
        public static System.IO.MemoryStream SerializeBinaryStream(object request)
        {
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer =
            new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            System.IO.MemoryStream memStream = new System.IO.MemoryStream();
            serializer.Serialize(memStream, request);
            return memStream;
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods")]
        public static object DeserializeBinaryStream(System.IO.Stream stream)
        {
            Guard.ArgumentNotNull(stream, "stream");

            stream.Position = 0;
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter deserializer =
            new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            object newobj = deserializer.Deserialize(stream);
            stream.Close();
            return newobj;
        }
        public static byte[] SerializeBinaryArray(object request)
        {
            return SerializeBinaryStream(request).ToArray();
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
        public static T DeserializeBinaryArray<T>(byte[] serialized)
        {
            return (T)DeserializeBinaryStream(new System.IO.MemoryStream(serialized));
        }

        #endregion
    }
}
